﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;
using MINERP.InfoConfig.Controller;
using MINERP.MODEL.InfoConfig;
using MINERP.DAL;
using System.Security.Cryptography;
using MINERP.COM;
namespace MINERP.InfoConfig
{
    public partial class FrmUserManager : FrmMDIChildFormTemplate
    {
        private UsersController _controller = new UsersController();
        private users _obj = null;


        static FrmUserManager instance = null;

        private FrmUserManager()
        {
            InitializeComponent();
        }

        public static FrmUserManager CreateInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmUserManager();
                }
                return instance;
            }
        }

        private void FrmUserManager_Load(object sender, EventArgs e)
        {
            _controller.Grid = gridControlUserInfo;
            _controller.View = gridViewUsers;
            _controller.GetDefaultData();
            _controller.ComoBox = cmbUserGroup;
            _controller.LinkUserGroup();
            _controller.CheckedComboBoxEdit = chkcmbRole;
            _controller.LinkchkcmbRole();

        }
        //
        private void gridViewUsers_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _obj = (users)gridViewUsers.GetRow(e.FocusedRowHandle);
            if (_obj != null)
            {
                txtLoginName.Text = _obj.LoginName;
                txtPWD.Text = _obj.LoginPwd;
                txtUserMaxTasks.Text = _obj.MaxTaskCount.ToString();
                txtUserName.Text = _obj.UserName;
                txtUserPhotoPath.Text = _obj.HeadPicPath;

                List<tb_Power> powerNameList = _obj.tb_Power.ToList();
                List<string> temlist = new List<string>();
                foreach (var item in powerNameList)
                {
            		 temlist.Add(item.Name);
                }
                _controller.CheckedComboBoxEdit = chkcmbRole;
                _controller.LinkchkcmbRole();
                foreach (CheckedListBoxItem item in chkcmbRole.Properties.Items)
                {
                    if (temlist.Contains(item.Value.ToString()))
                    {
                        item.CheckState = CheckState.Checked;
                    }
                }
                if (_obj.tb_WorkerGroup != null)
                {
                    cmbUserGroup.Text = _obj.tb_WorkerGroup.GroupName;
                }
                chkIsLeader.Checked = _obj.IsLeader;
                chkIsUsefull.Checked = _obj.IsUseful;

                _controller.XtraTabControlAction = xtraTabControl1;
                //_controller.CreateConctrolsOfUser(_obj);
                _controller.CreateConctrols(_obj.userID);
                _controller.XtraTabControl = xtraTabControl2;
                _controller.CreateConctrolTechsOfUser(_obj);
            }
        }
        //添加事件
        private void btnAddUser_Click(object sender, EventArgs e)
        {
            if (btnAddUser.Text != "保存")
            {
                PrepareAdd(true);
                txtLoginName.Focus();
                return;
            }
            if (CheckData())
            {
                users obj = new users();
                obj.LoginName = txtLoginName.Text.Trim();
                obj.LoginPwd = MD5HashCode.GetHash(txtPWD.Text);
                obj.MaxTaskCount = Convert.ToInt32(txtUserMaxTasks.Text);
                obj.UserName = txtUserName.Text;
                obj.HeadPicPath = txtUserPhotoPath.Text;
                List<CheckedListBoxItem> list = new List<CheckedListBoxItem>();
                foreach (CheckedListBoxItem item in chkcmbRole.Properties.Items)
                {
                    if (item.CheckState.ToString() == "Checked")
                    {
                        list.Add(item);
                    }
                }

                obj.IsLeader = chkIsLeader.Checked;
                obj.IsUseful = chkIsUsefull.Checked;
                string groupName = cmbUserGroup.Text;
                if (!_controller.checkUserIsIn(txtLoginName.Text.Trim()))
                    _controller.AddUser(obj, list, groupName);
                else
                {
                    MessageBox.Show("该用用户名已经存在，请重新输入再添加!");
                    return;
                }

                _controller.Grid = gridControlUserInfo;
                _controller.View = gridViewUsers;
                _controller.GetDefaultData();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //更新事件
        private void btnUpdateUser_Click(object sender, EventArgs e)
        {
            if (btnUpdateUser.Text != "保存")
            {
                PrepareUpdate(true);
                txtLoginName.Focus();
                return;
            }
            if (CheckData())
            {
                _obj.LoginName = txtLoginName.Text.Trim();
                users dbUser = _controller.GetUserObj(_obj.userID);
                if (dbUser.LoginPwd == txtPWD.Text)
                {
                    _obj.LoginPwd = txtPWD.Text;
                }
                else
                {
                    _obj.LoginPwd = MD5HashCode.GetHash(txtPWD.Text);
                }
                _obj.MaxTaskCount = Convert.ToInt32(txtUserMaxTasks.Text);
                _obj.UserName = txtUserName.Text;
                _obj.HeadPicPath = txtUserPhotoPath.Text;
                List<CheckedListBoxItem> list = new List<CheckedListBoxItem>();
                foreach (CheckedListBoxItem item in chkcmbRole.Properties.Items)
                {
                    if (item.CheckState == CheckState.Checked)
                    {
                        list.Add(item);
                    }
                }

                _obj.IsLeader = chkIsLeader.Checked;
                _obj.IsUseful = chkIsUsefull.Checked;
                string groupName = cmbUserGroup.Text;

                int result = _controller.UpdateUser(_obj, list, groupName);
                if (result == -1)
                {
                    MessageBox.Show("修改的用户名数据库已经存在，请重新输入用户名!");
                    return;
                }

                _controller.Grid = gridControlUserInfo;
                _controller.View = gridViewUsers;
                _controller.GetDefaultData();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        //删除事件
        private void btnDeleteUser_Click(object sender, EventArgs e)
        {
            if (_obj.LoginName.ToUpper() != "ADMIN")
            {
                if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    _controller.DeleteUser(_obj);

                    _controller.Grid = gridControlUserInfo;
                    _controller.View = gridViewUsers;
                    _controller.GetDefaultData();
                }
            }
            else
            {
                MessageBox.Show("系统数据不能删除", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }


        //取消事件

        private void btnUserCancel_Click(object sender, EventArgs e)
        {
            txtLoginName.Enabled = false; ;
            txtPWD.Enabled = false;
            txtUserMaxTasks.Enabled = false;
            txtUserName.Enabled = false;
            txtUserPhotoPath.Enabled = false;
            chkcmbRole.Enabled = false;
            chkIsLeader.Enabled = false;
            chkIsUsefull.Enabled = false;
            cmbUserGroup.Enabled = false;
            btnFolderbrowser.Enabled = false;

            btnAddUser.Text = "添加";
            btnUpdateUser.Text = "更新";
            btnAddUser.Enabled = true;
            btnUpdateUser.Enabled = true;
            btnDeleteUser.Enabled = true;
        }

        private void btnFolderbrowser_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "图片文件(*.JPG)|*.JPG|所有文件(*.*)|*.*";
            if (DialogResult.OK == openFileDialog1.ShowDialog())
            {

                if (openFileDialog1.FileName.Trim().Substring(0, 2) == @"\\")
                {
                    txtUserPhotoPath.Text = openFileDialog1.FileName;
                }
                else
                {
                    MessageBox.Show("请从网上邻居中选择网络路径!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtUserPhotoPath.Text = "";
                }
            }
        }
        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (string.IsNullOrEmpty(txtLoginName.Text.Trim()))
            {
                txtLoginName.Focus();
                MessageBox.Show("用户账号不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtPWD.Text == string.Empty)
            {
                txtPWD.Focus();
                MessageBox.Show("用户密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (txtUserPhotoPath.Text == string.Empty)
            {
                txtUserPhotoPath.Focus();
                MessageBox.Show("员工照片路径不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (txtUserName.Text == string.Empty)
            {
                txtUserName.Focus();
                MessageBox.Show("用户名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (chkcmbRole.Text == string.Empty)
            {
                chkcmbRole.Focus();
                MessageBox.Show("用户角色不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (cmbUserGroup.Text == string.Empty)
            {
                cmbUserGroup.Focus();
                MessageBox.Show("用户组不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (txtUserMaxTasks.Text == string.Empty)
            {
                txtUserMaxTasks.Focus();
                MessageBox.Show("最大任务量不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            txtLoginName.Enabled = bl;
            txtPWD.Enabled = bl;
            txtUserMaxTasks.Enabled = bl;
            txtUserName.Enabled = bl;
            txtUserPhotoPath.Enabled = bl;
            chkcmbRole.Enabled = bl;
            chkIsLeader.Enabled = bl;
            chkIsUsefull.Enabled = bl;
            cmbUserGroup.Enabled = bl;
            btnFolderbrowser.Enabled = bl;

            txtLoginName.Text = string.Empty;
            txtPWD.Text = string.Empty;
            txtUserMaxTasks.Text = string.Empty;
            txtUserName.Text = string.Empty;
            txtUserPhotoPath.Text = string.Empty;
            chkcmbRole.Text = string.Empty;
            cmbUserGroup.Text = string.Empty;
            chkIsLeader.Checked = false;
            chkIsUsefull.Checked = true;
            if (bl)
            {

                btnAddUser.Text = "保存";
                btnUpdateUser.Enabled = false;
                btnDeleteUser.Enabled = false;
            }
            else
            {
                btnAddUser.Text = "添加";
                btnUpdateUser.Enabled = true;
                btnDeleteUser.Enabled = true;
            }
        }
        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            txtLoginName.Enabled = bl;
            txtPWD.Enabled = bl;
            txtUserMaxTasks.Enabled = bl;
            txtUserName.Enabled = bl;
            txtUserPhotoPath.Enabled = bl;
            chkcmbRole.Enabled = bl;
            chkIsLeader.Enabled = bl;
            chkIsUsefull.Enabled = bl;
            cmbUserGroup.Enabled = bl;
            btnFolderbrowser.Enabled = bl;

            if (bl)
            {

                btnUpdateUser.Text = "保存";
                btnAddUser.Enabled = false;
                btnDeleteUser.Enabled = false;
            }
            else
            {
                btnUpdateUser.Text = "更新";
                btnAddUser.Enabled = true;
                btnDeleteUser.Enabled = true;
            }
        }

        private void chkcmbRole_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            _controller.CheckedComboBoxEdit = chkcmbRole;
            _controller.XtraTabControl = xtraTabControl2;
            _controller.CreateConctrolsTechs();
            _controller.GetTechList();
            _controller.XtraTabControlAction = xtraTabControl1;
            _controller.CreateConctrols(_obj.userID);

        }

        private void FrmUserManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }

        private void txtUserMaxTasks_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (txtUserMaxTasks.Text.Trim() != string.Empty)
                {
                    Convert.ToInt32(txtUserMaxTasks.Text);
                }
            }
            catch
            {
                MessageBox.Show("请输入数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUserMaxTasks.Text = string.Empty;
            }
        }

        private void xtraTabControl2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
